function getQueryStringArgs(){
  var qs = (location.search.length>0?location.search.substring(1):"");
  var args = {};
  var items = qs.length?qs.split("&"):[];
  var item = null;
  var name = null;
  var value = null;
  len = items.length;
  for (var i = 0; i < len; i++) {
    item = items[i].split("=");
    name = decodeURIComponent(item[0]);
    value = decodeURIComponent(item[1]);
    if (name.length) {
      args[name] = value;
    }
  }
  return args;
}

let args = getQueryStringArgs();

function loginWechat() {
  if(args['code']) {
    let wechatCode = args['code'];
    $.get('/user?code='+wechatCode,function(res){
      if(res.got) {
        $('#res-name').text(res.name);
        localStorage.name = res.name;
      } else {
        if (localStorage.name) {
          $('#res-name').text(localStorage.name);
        }
      }
    })
  } else {
    if (localStorage.name) {
      $('#res-name').text(localStorage.name);
    } else {
      location.href = 'http://longfor.ele233.com/wechat_login';
    }
  }
}
var wx= (function(){
  return navigator.userAgent.toLowerCase().indexOf('micromessenger') !== -1
})();
if(wx){
  console.log('微信内打开');
  loginWechat();
}else {
  console.log('非微信内打开');
}
